package com.aaa.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.aaa.service.DeptService;
import com.aaa.util.FileUtil;

@Controller
@RequestMapping("dept")
public class DeptController {
    @Autowired
    private DeptService deptService;
    @Autowired
    private ResourceLoader resourceLoader;

    @Value("${upload.path}")
    private String filePath; // D:/images/

    @Autowired
    public DeptController(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }
    /**
     * 部门列表
     * @param model
     * @return
     */
    @RequestMapping("toList")

    public String toList(Model model){
        List<Map> list = deptService.selectList();
        model.addAttribute("deptList", list);
        return "kuangjia/tupian";
    }

    @RequestMapping("getall")
@ResponseBody
    public String getall(){
        List<Map> data =deptService.selectList();
        Map zmap = new HashMap();
        zmap.put("options",data);
        System.out.println("options+++"+zmap);
        return "kuangjia/tupian";
    }

    /**
     * 图片显示方法
     * @param fileName
     * @return
     */
    @RequestMapping("show")
    public ResponseEntity show(String fileName){
        try {
            // 由于是读取本机的文件，file是一定要加上的， path是在application配置文件中的路径
            Resource resource = resourceLoader.getResource("file:" + filePath + fileName);
            System.out.println(resource.getURI()+","+resource.getURL()+","+resource.getFilename());
            System.out.println(ResponseEntity.ok(resourceLoader.getResource("file:" + filePath + fileName)).toString());
            System.out.println("11");
            return ResponseEntity.ok(resourceLoader.getResource("file:" + filePath + fileName));
        } catch (Exception e) {
            System.out.println("1122");
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 跳转添加
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAdd(){
        return "dept/add";
    }
    /**
     * 批量导入
     * @return
     */
    @RequestMapping("/batchAdd")
    public String batchAdd(@RequestParam MultipartFile excelFile){
        deptService.batchAdd(excelFile);
        return "redirect:toList";
    }

    /**
     * 添加
     * @param map
     * @return
     */
/*    @RequestMapping("add")
    public String add(@RequestParam Map map, @RequestParam MultipartFile pic){
     if(pic!=null&&!pic.isEmpty()){
            String s = FileUtil.uploadFile(filePath, pic);
            map.put("filePath",s);
            map.put("fileName",pic.getOriginalFilename());
        }
        System.out.println(map+"sssssssssssssss");
        deptService.add(map);
        return "redirect:toList";
    }*/
  /* @RequestMapping("add2")
    public String add(@RequestParam Map map){


        System.out.println(map+"sssssssssssssss");
      //  deptService.add(map);
        return "redirect:toList";
    }*/
    @RequestMapping("/add2")
    @ResponseBody
    public int add(@RequestBody Map map){
        //添加学习资料
        System.out.println(map+"sssssssssssssss");
        int index = deptService.add(map);
        //跳转到查询页面
        return index;
    }
    @RequestMapping("/downLoad")
    public void downFile(String fileName, HttpServletResponse response){
        FileUtil.downloadFile(filePath,fileName,response);
    }
    @RequestMapping("add")
    @ResponseBody
    public Object upload(@RequestParam("file") MultipartFile file){
        String upFile = null;
        if(file!=null&&!file.isEmpty()){
            //获取文件路径
            upFile = FileUtil.uploadFile(filePath, file);
            System.out.println("上传路径"+filePath+"   "+file);
            //获取上传文件的名字
            String fileName=file.getOriginalFilename();
        }
        System.out.println(upFile);
        return upFile;
    }

}
